﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using ECS.DAL;
using ECS.DAL.Component;
using ECS.BLL.Entity;
using ECS.Framework;
namespace ECS.BLL.Component
{
    public class ResultCheckMAWB
    {
        public CHECK_MAWB_TYPE Status { get; set; }
        public DataSet Data { get; set; }
    }
    public enum CHECK_MAWB_TYPE
    {
        Add,
        Search,
        None,
        Fail,
        Pass,
        Warning
    }
    class AddMAWB
    {
        #region private properties
        #endregion
        #region private methods
        private ResultCheckMAWB getConsol(string status, string hawb, DataSet ds, string datasource)
        {
            ResultCheckMAWB result = new ResultCheckMAWB();
            DataSet dsHawb = null;
            if (status == "PASS" && datasource == "LOCAL")
            {
                dsHawb = CheckHAWB(hawb, ds);
                if (dsHawb != null)
                {
                    result.Status = CHECK_MAWB_TYPE.Search;
                    result.Data = dsHawb;
                }
                else
                {
                    result.Status = CHECK_MAWB_TYPE.Add;
                }
            }
            else
            {
                result.Status = CHECK_MAWB_TYPE.Add;
            }
            return result;
        }
        private ResultCheckMAWB getNonConsol(string status, DataSet ds, string datasource)
        {
            ResultCheckMAWB result = new ResultCheckMAWB();
            if (status == "PASS" && datasource == "LOCAL")
            {
                    result.Status = CHECK_MAWB_TYPE.Search;
                    result.Data = ds;
            }
            else
            {
                result.Status = CHECK_MAWB_TYPE.Add;
            }
            return result;
        }
        #endregion
        #region public methods
        public ResultCheckMAWB CheckMAWB(string MAWB, string prefix, string HAWB, string serial, string shipmenttype, string UserID, string Device, string AppID)
        {
            string stype = string.Empty;
            string status = string.Empty;
            string datasource = string.Empty;
            string hawb = string.Empty;
            ResultCheckMAWB result = new ResultCheckMAWB();
            DataSet ds = new DataSet();
            using (Entities entity = new Entities())
            {
                StringBuilder sqlString = new StringBuilder();
                sqlString.Append("usp_GetMAWB");
                entity.Open();
                DbCommand cmd = entity.CreateCommand(sqlString.ToString());
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.Add(entity.CreateParameter("@in_vchApplicationID", AppID));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchDevice", Device));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchUserID", UserID));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchMAWB", MAWB));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchMAWBPrefix", prefix));
                cmd.Parameters.Add(entity.CreateParameter("@in_vchMAWBSerial", serial));
                if (shipmenttype == "")
                {
                    cmd.Parameters.Add(entity.CreateParameter("@in_vchHAWB", HAWB));
                }
                DbDataAdapter adapter = entity.CreateDataAdapter(cmd);
                adapter.Fill(ds);
                status = ds.Tables[0].Rows[0]["Status"].ToString();
                if (status == "PASS") //reult from store procedure
                {
                    stype = ds.Tables[0].Rows[0]["shipment_type"].ToString();
                    datasource = ds.Tables[0].Rows[0]["source"].ToString();
                    if (shipmenttype == "CONSOL") // radio button
                    {
                        if (stype == "CONSOL" ) // getShipment type
                        {
                            result=getConsol(status, HAWB, ds, datasource);
                        }
                        else if (stype == "")
                        {
                            result=getConsol(status, HAWB, ds, datasource);
                        }
                        else if(datasource == "CARGOSPOT")
                        {
                            if (CommonMessageBox.ShowMessageConfirmYN("This MAWB is already in CARGOSPOT. Are you want to add?")== DialogResult.Yes)
                            {
                               result= getConsol(status, HAWB, ds, datasource);
                            }
                            else
                            {
                                result.Status = CHECK_MAWB_TYPE.None;
                            }
                        }
                        else
                        {
                            result.Status = CHECK_MAWB_TYPE.None;
                            CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));                  
                        }
                    }
                    else // radio button
                    {
                        if (stype == "NONCON" && datasource == "LOCAL") 
                        {
                            if (!string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(serial))
                            {
                                result = getNonConsol(status, ds, datasource);
                            }
                            else
                            {
                                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_MAWB"));
                            }
                        }
                        else if (stype == "")
                        {
                            if (!string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(serial))
                            {
                                result=getNonConsol(status, ds, datasource);
                            }
                            else
                            {
                                CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_MAWB"));
                            }
                        }
                        else if (stype == "NONCON" && datasource == "CARGOSPOT")
                        {
                            if (CommonMessageBox.ShowMessageConfirmYN("This MAWB is already in CARGOSPOT. Are you want to add?") == DialogResult.Yes)
                            {
                                result = getNonConsol(status, ds, datasource);
                            }
                            else
                            {
                                result.Status = CHECK_MAWB_TYPE.None;
                            }
                        }
                        else
                        {
                            result.Status = CHECK_MAWB_TYPE.None;
                            CommonMessageBox.ShowMessageWarning(CommonResourceString.GetResourseValue("scn_Main2_Msgbox_Check_Shipment_type"));
                        }
                    }
                }
            }
            return result; 
        }
        public DataSet CheckHAWB(string hawb, DataSet ds)
        {
            //bool result = false;
            DataSet data = null;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (hawb == ds.Tables[0].Rows[i]["hawb"].ToString())
                {
                    //result = true;
                    data = new DataSet();
                    //var desRow = ds.Tables[0].NewRow();
                    //var srcRow = ds.Tables[0].Rows[i];
                    //desRow.ItemArray = srcRow.ItemArray.Clone() as object[];
                    DataTable dt = new DataTable("test");
                        dt = ds.Tables[0].Clone();
                        DataRow dr = ds.Tables[0].Rows[i];
                        dt.ImportRow(dr);
                    data.Tables.Add(dt);
                    break;
                }
            }
            return data;
        }
        #endregion
    }
}
